package com.kky.pp.popup

import android.app.Activity
import android.content.Context
import android.os.Build
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.kky.pp.common.Constant
import com.kky.pp.R
import com.kky.pp.adapter.PageBuyMemberAdapter
import com.kky.pp.entity.MemberSubscribeEntity
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.kky.pp.adapter.MemberSubscriptionAdapter
import com.youth.banner.Banner
import com.youth.banner.listener.OnPageChangeListener
import razerdp.basepopup.BasePopupWindow

class MemberBuyPopup(
    var ctx: Context,
    var currentItem: Int,
    var listener: ChangeMembershipListener
) : BasePopupWindow(ctx) {
    init {
        setContentView(R.layout.popup_member_buy)
        initView()
    }

    private var refreshTime: Long = 0
    private fun initView() {
        val dialogCycleBanner = findViewById<Banner<*, *>>(R.id.bannerPopupBuyVip)
        val dialogPurchaseNow = findViewById<TextView>(R.id.txtPopupBuyVipNow)
        val dialogCancel = findViewById<ImageView>(R.id.txtPopupBuyVipClose)

        val quarterContainer = findViewById<ConstraintLayout>(R.id.conPopupBuyVipTimesContainer)
        val itemQuarterTitle = findViewById<TextView>(R.id.txtPopupBuyVipTimesTitle)
        val itemQuarterNumber = findViewById<TextView>(R.id.txtPopupBuyVipTimesNumber)
        val itemQuarterName = findViewById<TextView>(R.id.txtPopupBuyVipTimesName)
        val itemQuarterDiscountedPrice = findViewById<TextView>(R.id.txtPopupBuyVipTimesPrice)

        val monthlyContainer = findViewById<ConstraintLayout>(R.id.conPopupBuyVipMonthlyContainer)
        val itemMonthlyTitle = findViewById<TextView>(R.id.txtPopupBuyVipMonthlyTitle)
        val itemMonthlyNumber = findViewById<TextView>(R.id.txtPopupBuyVipMonthlyNumber)
        val itemMonthlyName = findViewById<TextView>(R.id.txtPopupBuyVipMonthlyName)
        val itemMonthlyDiscountedPrice = findViewById<TextView>(R.id.txtPopupBuyVipMonthlyPrice)

        val weeksContainer = findViewById<ConstraintLayout>(R.id.conPopupBuyVipWeeksContainer)
        val itemWeeksNumber = findViewById<TextView>(R.id.txtPopupBuyVipWeeksNumber)
        val itemWeeksName = findViewById<TextView>(R.id.txtPopupBuyVipWeeksName)
        val itemWeeksDiscountedPrice = findViewById<TextView>(R.id.txtPopupBuyVipWeeksPrice)

        val tvTipTitle = findViewById<TextView>(R.id.txtPopupBuyVipTipTitle)
        val tvTipContent = findViewById<TextView>(R.id.txtPopupBuyVipContent)
        val itemIndicatorRg = findViewById<RadioGroup>(R.id.rgPopupBuyVipIndicator)
        val ivEquityList = findViewById<ImageView>(R.id.imgViewPopupBuyVipEquityList)
        val nestedScrollView = findViewById<NestedScrollView>(R.id.nestedScrollView)
        if (itemIndicatorRg.getChildAt(currentItem) != null) {
            (itemIndicatorRg.getChildAt(currentItem) as RadioButton).isChecked = true
        }
        val memberEquityList = findViewById<RecyclerView>(R.id.rvViewPopupBuyVipEquityList)
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(Constant.user_member_product_category_url)
                requestBody.add("productCategory", 1)
            }

        }, object : SDOkHttpResoutCallBack<MemberSubscribeEntity>() {
            override fun onSuccess(entity: MemberSubscribeEntity) {
                val subscriptions = entity.data.subscriptions
                if (subscriptions.size != itemIndicatorRg.childCount) {
                    itemIndicatorRg.removeViews(
                        subscriptions.size,
                        itemIndicatorRg.childCount - subscriptions.size
                    )
                }

                memberEquityList.layoutManager = LinearLayoutManager(ctx)
                memberEquityList.adapter = MemberSubscriptionAdapter(subscriptions, ctx as Activity)

                dialogCycleBanner.setStartPosition(currentItem+1)
                dialogCycleBanner.adapter = PageBuyMemberAdapter(ctx, subscriptions)
                dialogPurchaseNow.setOnClickListener {
                    var bean: MemberSubscribeEntity.Data.ProductDescriptions? = null
                    for (i in 0 until entity.data.productDescriptions.size) {
                        if (entity.data.productDescriptions[i].check) {
                            bean = entity.data.productDescriptions[i]
                            break
                        }
                    }
                    bean?.let { it1 -> listener.onListener(it1) }
                    dismiss()
                }

                val bean = entity.data
                bean.productDescriptions[0].popButtonTitle?.let {
                    tvTipTitle.text=it
                }
                bean.productDescriptions[0].popButtonContent?.let {
                    tvTipContent.text=it
                }
                quarterContainer.setBackgroundResource(if (bean.productDescriptions[2].check) R.drawable.shape_member_dialog_select_title_bg else R.drawable.shape_a7508b_radius_10)
                itemQuarterTitle.text = bean.productDescriptions[2].tip
                itemQuarterNumber.text = bean.productDescriptions[2].benefitNum
                itemQuarterName.text = bean.productDescriptions[2].benefitUnit
                itemQuarterDiscountedPrice.text = "$"+bean.productDescriptions[2].price

                monthlyContainer.setBackgroundResource(if (bean.productDescriptions[1].check) R.drawable.shape_member_dialog_select_title_bg else R.drawable.shape_a7508b_radius_10)
                itemMonthlyTitle.text = bean.productDescriptions[1].tip
                itemMonthlyNumber.text = bean.productDescriptions[1].benefitNum
                itemMonthlyName.text = bean.productDescriptions[1].benefitUnit
                itemMonthlyDiscountedPrice.text = "$"+bean.productDescriptions[1].price

                weeksContainer.setBackgroundResource(if (bean.productDescriptions[0].check) R.drawable.shape_member_dialog_select_title_bg else R.drawable.shape_a7508b_radius_10)
                itemWeeksNumber.text = bean.productDescriptions[0].benefitNum
                itemWeeksName.text = bean.productDescriptions[0].benefitUnit
                itemWeeksDiscountedPrice.text = "$"+bean.productDescriptions[0].price

                quarterContainer.setOnClickListener {
                    quarterContainer.setBackgroundResource(R.drawable.shape_member_dialog_select_title_bg)
                    bean.productDescriptions[2].check = true

                    monthlyContainer.setBackgroundResource(R.drawable.shape_a7508b_radius_10)
                    bean.productDescriptions[1].check = false

                    weeksContainer.setBackgroundResource(R.drawable.shape_a7508b_radius_10)
                    bean.productDescriptions[0].check = false
                }

                monthlyContainer.setOnClickListener {
                    quarterContainer.setBackgroundResource(R.drawable.shape_a7508b_radius_10)
                    bean.productDescriptions[2].check = false

                    monthlyContainer.setBackgroundResource(R.drawable.shape_member_dialog_select_title_bg)
                    bean.productDescriptions[1].check = true

                    weeksContainer.setBackgroundResource(R.drawable.shape_a7508b_radius_10)
                    bean.productDescriptions[0].check = false

                }

                weeksContainer.setOnClickListener {
                    quarterContainer.setBackgroundResource(R.drawable.shape_a7508b_radius_10)
                    bean.productDescriptions[2].check = false

                    monthlyContainer.setBackgroundResource(R.drawable.shape_a7508b_radius_10)
                    bean.productDescriptions[1].check = false

                    weeksContainer.setBackgroundResource(R.drawable.shape_member_dialog_select_title_bg)
                    bean.productDescriptions[0].check = true
                }
                entity.data.refreshTime?.let {
                    this@MemberBuyPopup.refreshTime = it
                }
                showPopupWindow()
            }
        })

        dialogCancel.setOnClickListener {
            listener.closeListener(refreshTime)
            dismiss()
        }
        dialogCycleBanner.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {

            }

            override fun onPageSelected(p0: Int) {
                for (i in 0 until itemIndicatorRg.childCount) {
                    (itemIndicatorRg.getChildAt(i) as RadioButton).isChecked = p0 == i
                }
            }

            override fun onPageScrollStateChanged(p0: Int) {

            }

        })


        setOutSideDismiss(true)
    }

    interface ChangeMembershipListener {
        fun onListener(bean: MemberSubscribeEntity.Data.ProductDescriptions)
        fun closeListener(refreshTime: Long)
    }

}